<?php
/**
 * Created by PhpStorm.
 * User: lenovo-7
 * Date: 2015/6/16
 * Time: 11:37
 */

class inman_api_register extends inman_api_base{
    /**
     * 1.7	手机注册-发送验证码
     * @param phone int 手机号
     * @param password int 密码
     * @return vcode 验证码
     */
    public function sendVerify($params,&$service){
        $params = utils::_filter_input($params);//过滤xss攻击
        if( empty($params['phone']) ){
            $error['status'] = 'false';
            $error['message'] = app::get('b2c')->_('手机号码不能为空');
            return $error;
        }
        if( empty($params['password']) ){
            $error['status'] = 'false';
            $error['message'] = app::get('b2c')->_('密码不能为空');
            return $error;
        }
        $userPassport = kernel::single('b2c_user_passport');
        $res = $userPassport->check_signup_account($params['phone'],$msg);
        if( !$res ){
            $error['status'] = 'false';
            $error['message'] = $msg;
            return $error;
        }
        if( $msg != 'mobile' ){
            $error['status'] = 'false';
            $error['message'] = app::get('b2c')->_('请填写正确的手机号码');
            return $error;
        }
        if( !$userPassport->check_passport($params['password'],$params['password'],$msg) ){
            return $service->send_user_error($msg);
        }
        $data = $this->send_sms($params['phone'],'signup',$msg);
        if( !$data ){
            $error['status'] = 'false';
            $error['message'] = $msg;
            return $error;
        }
        kernel::single("base_session")->start();
        $return['sid']=kernel::single("base_session")->sess_id();
        $return['vcode'] = $data['vcode'];
        $_SESSION['user_signup']['vcode'] = $data['vcode'];
        $_SESSION['user_signup']['phone'] = $params['phone'];
        $_SESSION['user_signup']['password'] = $params['password'];
        return $return;
    }

    /**
     * 1.8	手机注册-再次发送验证码
     */
    public function sendVerifyAgain($params,&$service)
    {
        $params = utils::_filter_input($params);//过滤xss攻击
        $_GET['sess_id'] = $params['sid'];
        kernel::single("base_session")->start();
        $vcode = $_SESSION['user_signup']['vcode'];
        $phone = $_SESSION['user_signup']['phone'];
        if(!$vcode){
            $msg = app::get('b2c')->_('sid不正确或验证码过期');
            $error['status'] = 'false';
            $error['message'] = $msg;
            return $error;
        }
        $userVcode = kernel::single('b2c_user_vcode');
        $data['vcode'] = $vcode;
        if( !$userVcode->send_sms('signup',(string)$phone,$data) ){
            app::get('b2c')->_('短信发送失败');
            return false;
        }
        $return['error_code'] = 0;
        return $return;
    }

    /**
     * 1.9	手机注册-检查验证码
     */
    public function checkVerify($params,&$service){
        $params = utils::_filter_input($params);//过滤xss攻击
        $_GET['sess_id'] = $params['sid'];
        kernel::single("base_session")->start();
        $vcode = $_SESSION['user_signup']['vcode'];
        $phone = $_SESSION['user_signup']['phone'];
        $password = $_SESSION['user_signup']['password'];
        if(!$vcode){
            $msg = app::get('b2c')->_('sid不正确或验证码过期');
            $error['status'] = 'false';
            $error['message'] = $msg;
            return $error;
        }

        $userPassport = kernel::single('b2c_user_passport');

        $res = $userPassport->check_signup_account($phone,$msg);
        if( !$res ){
            $error['status'] = 'false';
            $error['message'] = $msg;
            return $error;
        }

        if( $msg == 'mobile' ){
            if( $params['verify'] != $vcode ){
                $msg = app::get('b2c')->_('短信验证错误');
                $error['status'] = 'false';
                $error['message'] = $msg;
                return $error;
            }
        }

        $saveData = $userPassport->pre_signup_process($process_data);
        $process_data['login_name'] = $phone;
        $process_data['login_password'] = $password;
        $saveData['b2c_members']['source'] = 'api';
        $pamAccount = $userPassport->pre_account_signup_process($process_data);
        $saveData['pam_account'] = $pamAccount;

        if( $member_id = $userPassport->save_members($saveData,$msg) ){
            foreach(kernel::servicelist('b2c_save_post_om') as $object) {
                $object->set_arr($member_id, 'member');
                $refer_url = $object->get_arr($member_id, 'member');
            }
            /*注册完成后做某些操作! begin*/
            foreach(kernel::servicelist('b2c_register_after') as $object) {
                $object->registerActive($member_id);
            }
            //增加会员同步 2012-5-15
            if( $member_rpc_object = kernel::service("b2c_member_rpc_sync") ) {
                $member_rpc_object->createActive($member_id);
            }
            /*end*/
            $db = kernel::database();
            kernel::single("base_session")->destory();
            unset($_GET['sess_id']);
            $account = app::get('pam')->model('members')->getList('*',array('member_id'=>$member_id));
            $member_data = app::get('b2c')->model('members')->getRow( '*', array('member_id'=>$member_id) );
            $next_rank_integral = $db->selectrow('select point from sdb_b2c_member_lv where member_lv_id = '.($member_data['member_lv_id']+1));
            $next_rank_name = $db->selectrow('select name from sdb_b2c_member_lv where member_lv_id = '.($member_data['member_lv_id']+1));
            $user_rank_name = $db->selectrow('select name from sdb_b2c_member_lv where member_lv_id='.$member_data['member_lv_id']);

            $data['sid'] = $this->set_sid($member_id);
            $data['user_id'] = $account[0]['member_id'];
            $data['user_name'] = $account[0]['login_account'];
            $data['user_avatar'] = $member_data['user_avatar'];
            $data['user_integral'] = $member_data['point'];
            $data['user_rank'] = $member_data['member_lv_id'];
            $data['user_rank_desc'] = !$next_rank_name?'您目前是：['.$user_rank_name['name'].'].':'您目前是：['.$user_rank_name['name'].']（积分累积到'.$next_rank_integral['point'].'可成为'.$next_rank_name['name'].'）';
            $data['is_union'] = false;
            $data['is_bind'] = !empty($member_data['mobile']);
            $data['bind_number'] = empty($member_data['mobile'])?'':$member_data['mobile'];
            $data['is_pass'] = false;

            kernel::single('b2c_user_object')->set_member_session($member_id);
            return $data;
        }else{
            return $service->send_user_error($msg);
        }
    }
}